package com.faxsun.api.wrapper.product;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.broadleafcommerce.common.file.service.StaticAssetPathService;
import org.broadleafcommerce.common.media.domain.Media;
import org.broadleafcommerce.common.money.Money;
import org.broadleafcommerce.core.catalog.domain.Product;
import org.broadleafcommerce.core.web.api.wrapper.MediaWrapper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.faxsun.core.catalog.domain.FSProductImpl;
import com.faxsun.core.catalog.domain.FSSkuImpl;


@Component("fsProductWrapper")
@Scope("prototype")
@XmlRootElement(name = "fsProduct")
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlType(name="fsProductResource")
public class ProductWrapper extends org.broadleafcommerce.core.web.api.wrapper.ProductWrapper {
	
	@XmlElement
	protected String country;
	
	@XmlElement
	protected Money marketPrice;
	
	@XmlElement
	protected Money shippmentFee;
	
	@XmlElement
	protected Money displayedTotalPrice;
	
	@XmlElement
	protected MediaWrapper tvMedia;
	
	@Override
	public void wrapDetails(Product model, HttpServletRequest request) {
		super.wrapDetails(model, request);
		String description = super.longDescription;
		String url = "";
		if (request.getContextPath() != null && ! "".equals(request.getContextPath()) && !description.isEmpty()) {
            url = request.getContextPath();
            String pathHeader = ProductConstants.CMS_STATIC;
    	    CharSequence prefix = (CharSequence)(url+pathHeader);
    	    String replacedStr = description.replace(pathHeader, prefix);
    	    CharSequence jpg = ProductConstants.JPG_APPENDIX;
    	    CharSequence suffix = jpg+ProductConstants.BROWSE_APPENDIX;
    	    replacedStr = replacedStr.replace(jpg, suffix);
    	    super.longDescription = replacedStr;
        }				
		this.country = ((FSProductImpl) model).getCountry().getType();
		// 选取default SKU 的marketPrice 作为标示的市场价
		Money market = ( (FSSkuImpl) ( (FSProductImpl) model).getDefaultSku()).getMarketPrice();
		if(market != null){
			this.marketPrice = market;
		}

		this.shippmentFee = ( (FSSkuImpl) model.getDefaultSku()).getCalculatedShippmentFee();		
		this.displayedTotalPrice = ((FSSkuImpl) model.getDefaultSku()).getDisplayedTotalPrice();
		
	}

	@Override
	public void wrapSummary(Product model, HttpServletRequest request) {	
		super.wrapSummary(model, request);
		super.productOptions = null;
		super.longDescription = null;
		this.shippmentFee = ( (FSSkuImpl) model.getDefaultSku()).getCalculatedShippmentFee();	
		// for home page of TV
		if (model.getMedia() != null && !model.getMedia().isEmpty()) {
			Media media = model.getMedia().get(ProductConstants.TV);
			if(media != null){
				StaticAssetPathService staticAssetPathService = (StaticAssetPathService) this.context.getBean("blStaticAssetPathService");
				tvMedia = (MediaWrapper) context.getBean(MediaWrapper.class.getName());
				tvMedia.wrapDetails(media, request);
				if (tvMedia.isAllowOverrideUrl()) {
					tvMedia.setUrl(staticAssetPathService.convertAssetPath(media.getUrl(), request.getContextPath(), request.isSecure()));
	            }	
			}
		}
		
		this.country = ((FSProductImpl) model).getCountry().getType();
		this.shippmentFee = ( (FSSkuImpl) model.getDefaultSku()).getCalculatedShippmentFee();	
		this.displayedTotalPrice = ((FSSkuImpl) model.getDefaultSku()).getDisplayedTotalPrice();
	}
	
}
